package yto.freemarker.springbootstudy.service;

import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yto.freemarker.springbootstudy.entity.App;
import yto.freemarker.springbootstudy.mapper.AppMapper;

import java.util.List;

/**
 * @author dongxing
 * @create 2018-02-26 13:39
 **/
@Service
@AllArgsConstructor(onConstructor = @_(@Autowired))
public class AppServiceImpl {

    private final AppMapper appMapper;

//    private final JedisCluster jedisCluster;
//
//    public String findRedis() {
//        jedisCluster.set("username", "你还");
//        String username = jedisCluster.get("username");
//        return username;
//    }


    /**
     * 在redis中储存了一个key:findAll,value:就是查询的结果
     *
     * @return
     */
//    @Cacheable(value = "findAll")
//    public List<App> findAll() {
//        System.out.println("如果第二次没有打印此文件，则说明，缓存存在");
//        return appMapper.selectAll();
//    }
    @Transactional
    public List<App> updateData() throws RuntimeException {
        try {
            appMapper.updateByPrimaryKey(App.builder().userId(1).userName("zhjansgan").build());
            int x = 1 / 0;
            appMapper.updateByPrimaryKeySelective(App.builder().userId(2).userName("lisi").build());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("运行报错！");
        }

        return appMapper.selectAll();
    }

    public List<App> findAll() {
        List<App> all = appMapper.findAll();
        return all;
    }


}
